/*******************************************************************************
 * ATE, Automation Test Engine
 *
 * Copyright 2015, Montreal PROT, or individual contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Montreal PROT.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package testsuite_login;

import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

import pages.Homepage;
import base.BaseScript;

// TODO: Auto-generated Javadoc
/**
 * The Class Login.
 */
public class Login extends BaseScript {//NOPMD
	
	/**
	 * Wait2.
	 */
	private void wait2() {
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();//NOPMD
		}
	}

	/**
	 * Back to homepage.
	 */
	@AfterMethod
	public void backToHomepage() {
		driver.get("http://www.bigtester.com");
		wait2();
	}

	//@Test(priority = 1)
	/**
	 * False login better code example for pom.
	 */
	public void falseLoginBetterCodeExampleForPOM() {
		String username = "bigtester"; //NOPMD
		String password = "bigtester";
		Reporter.log("test case0001-false login page factory example: ");
		try {
			Homepage nlHomepage = new Homepage(driver);

			nlHomepage.getLoginForm().typeUsername(username);
			wait2();

			nlHomepage.getLoginForm().typePassword(password);
			wait2();

			nlHomepage.getLoginForm().submitFalseLogin();

		} catch (AssertionError E) {
			throw new IllegalStateException(E.getMessage() + " in Test case 'false login better example'"); //NOPMD
		}

	}

	//@Test(priority = 3)
	/**
	 * False login page factory example.
	 */
	public void falseLoginPageFactoryExample() {
		String username = "bigtester";
		String password = "bigtester";
		Reporter.log("test case0003-false login page factory example: ");

		Reporter.log("homepage step start->");
		Homepage nlHomepage = new Homepage(driver);
		Reporter.log("homepage step end->");

		Reporter.log("login username type start->");
		nlHomepage.getPFLoginForm().typeUsername(username);
		Reporter.log("login username type end->");

		wait2();

		Reporter.log("login passwork type start->");
		nlHomepage.getPFLoginForm().typePassword(password);
		Reporter.log("login passwork type end->");

		wait2();

		Reporter.log("login submit button click start->");
		nlHomepage.getPFLoginForm().submitFalseLogin();
		Reporter.log("login submit button click end->");

	}

	/**
	 * Flase login bad code example for pom.
	 */
	@Test(priority = 2)
	public void flaseLoginBadCodeExampleForPOM() {
		Reporter.log("test case0002-false login page factory example: ");
		String username = "bigtester";
		String password = "bigtester";
		By usernameLocator = By.id("modlgn_username");
		By passwordLocator = By.id("modlgn_passwd");
		By loginButtonLocator = By.name("Submit"); //NOPMD
		driver.findElement(usernameLocator).sendKeys(username);
		wait2();
		driver.findElement(passwordLocator).sendKeys(password);
		wait2();
		driver.findElement(loginButtonLocator).click();
		Assert.assertTrue(
				driver.getPageSource()
						.contains(
								"Username and password do not match or you do not have an account yet1"),
				"False Login page not appear");

	}

	/**
	 * {@inheritDoc}
	 */
	@BeforeTest
	public void initTest() {
		super.initTest();
	}

	/**
	 * {@inheritDoc}
	 */
	@AfterTest
	public void finalizeTest() {
		super.finalizeTest();
	}

}
